﻿<UserControl x:Class="Passwords.Views.EditPasswordView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:utils="clr-namespace:Passwords.Utils"
        xmlns:views="clr-namespace:Passwords.Views" xmlns:vm="clr-namespace:Passwords.ViewModels"
        xmlns:models="clr-namespace:Passwords.Models" xmlns:controls="clr-namespace:Passwords.Controls"
        xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:vr="clr-namespace:Passwords.ValidationRules"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" mc:Ignorable="d" d:DesignHeight="581"
        d:DesignWidth="846">
    <UserControl.InputBindings>
        <KeyBinding Key="Esc" Command="{x:Static vm:Commands.CancelPasswordCommand}" />
        <KeyBinding Key="F11" Command="{x:Static vm:Commands.SavePasswordCommand}" />
        <KeyBinding Key="Enter" Command="{x:Static vm:Commands.SavePasswordCommand}" />
        <KeyBinding Key="Enter" Modifiers="Ctrl" Command="{x:Static vm:Commands.SavePasswordCommand}" />
        <KeyBinding Key="N" Modifiers="Ctrl" Command="{x:Static vm:Commands.AddFieldCommand}" />
        <KeyBinding Key="F" Modifiers="Ctrl" Command="{x:Static vm:Commands.AddFieldCommand}" />
    </UserControl.InputBindings>
    <UserControl.Resources>
        <ExponentialEase Exponent="5" x:Key="ease" EasingMode="EaseOut" />

        <ControlTemplate x:Key="template">
            <Grid>
                <Grid Margin="0,0,10,0" FocusManager.FocusedElement="{Binding ElementName=editName}" x:Name="grid">
                    <Grid.Resources>
                        <Style TargetType="CheckBox" BasedOn="{StaticResource checkBoxStyle}"/>
                        <Style TargetType="RadioButton" BasedOn="{StaticResource textRadioStyle}" />
                        <Style TargetType="Button" BasedOn="{StaticResource iconButtonStyle}" />

                        <ControlTemplate TargetType="Control" x:Key="pwsTemplateFieldTemplate">
                            <Grid Margin="4" HorizontalAlignment="Stretch">
                                <Grid.InputBindings>
                                    <KeyBinding Key="G" Modifiers="Ctrl" Command="{x:Static vm:Commands.OpenPasswordGen}"/>
                                </Grid.InputBindings>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="121" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="6" />
                                    <ColumnDefinition Width="auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="{Binding Name}" FontSize="12" VerticalAlignment="Top"
                                    TextAlignment="Right" Margin="0,4" />
                                <TextBox x:Name="editName" Grid.Column="1"
                                    Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" FontSize="14"
                                    HorizontalAlignment="Stretch" Margin="10,0,0,0"
                                    TextWrapping="Wrap" />
                                <ProgressBar  Grid.Column="2"  Maximum="140" Value="{Binding Strength, Mode=OneWay}" Margin="0" Orientation="Vertical" Style="{StaticResource pwsAppProgressBarStyle}"/>
                                <Button Width="24" Grid.Column="3" Command="{x:Static vm:Commands.OpenPasswordGen}" Margin="0">
                                    <Image Source="/Passwords;component/Images/Lock.png" />
                                </Button>
                            </Grid>
                        </ControlTemplate>

                        <ControlTemplate TargetType="Control" x:Key="templateFieldTemplate">
                            <Grid Margin="4" HorizontalAlignment="Stretch">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="121" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="{Binding Name}" FontSize="12" VerticalAlignment="Top"
                                    TextAlignment="Right" Margin="0,4" />
                                <TextBox x:Name="editName" Grid.Column="1"
                                    Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" FontSize="14"
                                    HorizontalAlignment="Stretch" Margin="10,0,0,0" AcceptsReturn="True"
                                    TextWrapping="Wrap" />
                            </Grid>
                        </ControlTemplate>

                        <ControlTemplate TargetType="Control" x:Key="customFieldTemplate">
                            <Grid Margin="4" HorizontalAlignment="Stretch">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="121" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="auto" />
                                </Grid.ColumnDefinitions>
                                <TextBox Grid.Column="0" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
                                    FontSize="12" TextAlignment="Right" Padding="2,3,2,5" Margin="0,0,-6,0"
                                    VerticalAlignment="Top" />
                                <TextBox x:Name="editName" Grid.Column="1"
                                    Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" FontSize="14"
                                    HorizontalAlignment="Stretch" Margin="10,0,0,0" AcceptsReturn="True"
                                    TextWrapping="Wrap" />
                                <Button Grid.Column="2" Command="{x:Static vm:Commands.RemoveFieldCommand}"
                                    Focusable="False" VerticalAlignment="Top">
                                    <Image Source="/Passwords;component/Images/appbar.minus.rest.png" />
                                </Button>
                            </Grid>
                        </ControlTemplate>

                        <DataTemplate x:Key="editFieldTemplate">
                            <Control x:Name="item" Template="{StaticResource templateFieldTemplate}" Focusable="False" />
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsCustom}" Value="true">
                                    <Setter Property="Control.Template" Value="{StaticResource customFieldTemplate}"
                                        TargetName="item" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding IsPassword}" Value="true">
                                    <Setter Property="Control.Template" Value="{StaticResource pwsTemplateFieldTemplate}" TargetName="item"/>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>

                        <DataTemplate x:Key="editFieldTemplate2">
                            <Control x:Name="item" Template="{StaticResource templateFieldTemplate}" Focusable="False" />
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsCustom}" Value="true">
                                    <Setter Property="Control.Template" Value="{StaticResource customFieldTemplate}"
                                        TargetName="item" />
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>

                        <DataTemplate x:Key="categoryTemplate">
                            <CheckBox Content="{Binding Name}" FontSize="12" IsChecked="{Binding IsSelected}"
                                HorizontalAlignment="Right" Margin="4,2,15,2" />
                        </DataTemplate>

                    </Grid.Resources>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>

                    <!--Header:-->
                    <Grid Grid.Row="0" Grid.ColumnSpan="2" Margin="10,5,0,-5">
                        <Grid VerticalAlignment="Top" Margin="0,-15,20,20" TextBlock.FontSize="48" TextBlock.FontWeight="Light">
                            <TextBlock x:Name="title" Text="{Binding rs.EditPasswordTitle, Source={StaticResource locale}}" />
                            <TextBlock x:Name="newtitle" Text="{Binding rs.NewPasswordTitle, Source={StaticResource locale}}" Visibility="Collapsed" />
                        </Grid>
                        <StackPanel x:Name="appButtons" VerticalAlignment="Top" HorizontalAlignment="Right"
                            Margin="0,0,-4,0" Orientation="Horizontal">
                            <Button Content="{Binding rs.LogoutBtn, Source={StaticResource locale}}" Style="{StaticResource radioLikeButtonStyle}"
                                Command="{x:Static vm:Commands.LogoutCommand}" />
                            <StackPanel.RenderTransform>
                                <TranslateTransform x:Name="appButtonsTransform" />
                            </StackPanel.RenderTransform>
                        </StackPanel>
                    </Grid>

                    <!--Lower Content:-->
                    <Grid Grid.Row="1" MaxWidth="800">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <!--Group Headers:-->
                        <Border x:Name="categoriesHeader" Grid.Row="1" Grid.Column="1"
                            Background="{StaticResource TransparentChromeBrush}" Margin="0,0,0,0" Padding="0">
                            <TextBlock Text="{Binding rs.EditCategories, Source={StaticResource locale}}" Style="{StaticResource headerText}" HorizontalAlignment="Right"
                                Margin="5,0,15,2" />
                            <Border.RenderTransform>
                                <TranslateTransform x:Name="categoriesHeaderTransform" />
                            </Border.RenderTransform>
                        </Border>

                        <!--Groups:-->
                        <ScrollViewer x:Name="passwordContent" VerticalScrollBarVisibility="Auto"
                            HorizontalScrollBarVisibility="Disabled" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2"
                            Focusable="False" Margin="0,0,10,0">
                            <ScrollViewer.RenderTransform>
                                <TranslateTransform x:Name="passwordContentTransform" />
                            </ScrollViewer.RenderTransform>
                            <StackPanel>
                                <Grid Margin="0,0,10,0" HorizontalAlignment="Stretch">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="auto" />
                                        <RowDefinition Height="auto" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="121" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Column="0" Text="{Binding rs.EditName, Source={StaticResource locale}}" FontSize="16" VerticalAlignment="Stretch"
                                        TextAlignment="Right" />
                                    <TextBox Grid.Column="1" FontSize="16" HorizontalAlignment="Stretch" Margin="10,0,0,4"
                                        VerticalAlignment="Stretch" x:Name="nameBox">
                                        <TextBox.Text>
                                            <Binding Path="SelectedPassword.Name" UpdateSourceTrigger="PropertyChanged"
                                                ValidatesOnDataErrors="False" ValidatesOnExceptions="False"
                                                BindsDirectlyToSource="True">
                                                <!--remarks: this does not update the command to disable the save button so it is disabled here:-->
                                                <!--<Binding.ValidationRules>
                                    <vr:TextEmptyValidationRule/>
                                </Binding.ValidationRules>-->
                                            </Binding>
                                        </TextBox.Text>
                                    </TextBox>

                                    <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding rs.EditTemplate, Source={StaticResource locale}}" FontSize="12"
                                        VerticalAlignment="Center" Margin="0,3,0,0" TextAlignment="Right" />
                                    <ComboBox Grid.Column="1" Grid.Row="1" IsEnabled="{Binding IsTemplateUnlocked}" 
                                        SelectedItem="{Binding Template}" ItemsSource="{Binding Templates}"
                                        FontSize="16" FontWeight="Normal" DisplayMemberPath="Name" Margin="10,4,0,0"
                                        Padding="5,2" Tag="white" VerticalAlignment="Center" />
                                </Grid>
                                <ItemsControl x:Name="fieldList" Margin="-4,5,6, 10" Focusable="False"
                                    ItemsSource="{Binding EditFields}" ItemTemplate="{StaticResource editFieldTemplate}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel IsItemsHost="True">
                                                <i:Interaction.Behaviors>
                                                    <ei:FluidMoveBehavior AppliesTo="Children" Duration="0:0:0.25"
                                                        EaseY="{StaticResource fluidEase}" />
                                                </i:Interaction.Behaviors>
                                            </StackPanel>
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                </ItemsControl>

                                <Button x:Name="addFieldBtn" Content="{Binding rs.AddFieldBtn, Source={StaticResource locale}}" Width="NaN" Margin="131,0,10,0"
                                    HorizontalContentAlignment="Left" Template="{StaticResource addButtonTemplate}"
                                    Command="{x:Static vm:Commands.AddFieldCommand}">
                                    <Button.RenderTransform>
                                        <TranslateTransform x:Name="addFieldTransform" />
                                    </Button.RenderTransform>
                                </Button>


                                <i:Interaction.Behaviors>
                                    <ei:FluidMoveBehavior AppliesTo="Children" Duration="0:0:0.25"
                                        EaseY="{StaticResource fluidEase}" FloatAbove="True" />
                                </i:Interaction.Behaviors>
                            </StackPanel>
                        </ScrollViewer>
                        <ScrollViewer x:Name="categoriesContent" VerticalScrollBarVisibility="Auto" Grid.Column="1"
                            Grid.Row="2" Focusable="False">
                            <ScrollViewer.RenderTransform>
                                <TranslateTransform x:Name="categoriesContentTransform" />
                            </ScrollViewer.RenderTransform>
                            <ItemsControl x:Name="categoryList" Margin="10,10,-13,5" MinWidth="120" Focusable="False"
                                ItemsSource="{Binding SelectedCategories}"
                                ItemTemplate="{StaticResource categoryTemplate}" />
                        </ScrollViewer>
                    </Grid>

                    <!--Footer:-->
                    <Border BorderThickness="0,1,0,0" Grid.Row="2"  BorderBrush="{DynamicResource TransparentChromeBrush}">
                        <StackPanel x:Name="footerPanel"  Orientation="Horizontal" Margin="10,8,10,0"
                        HorizontalAlignment="Center">
                            <StackPanel.Resources>
                                <Style TargetType="Button" BasedOn="{StaticResource radioLikeButtonStyle}" />
                            </StackPanel.Resources>
                            <Button Content="{Binding rs.SaveBtn, Source={StaticResource locale}}" FontSize="14" Command="{x:Static vm:Commands.SavePasswordCommand}" />
                            <Separator Style="{StaticResource lineSepStyle}" />
                            <Button Content="{Binding rs.UndoBtn, Source={StaticResource locale}}" FontSize="14" Command="{x:Static vm:Commands.UndoPasswordCommand}" />
                            <Separator Style="{StaticResource lineSepStyle}" />
                            <Button Content="{Binding rs.CancelBtn, Source={StaticResource locale}}" FontSize="14" Command="{x:Static vm:Commands.CancelPasswordCommand}" />
                            <StackPanel.RenderTransform>
                                <TranslateTransform x:Name="footerTransform" />
                            </StackPanel.RenderTransform>
                        </StackPanel>
                    </Border>

                </Grid>
                <views:PasswordGenView Margin="-15"/>
            </Grid>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding SelectedPassword.IsNew}" Value="true">
                    <Setter TargetName="title" Property="Visibility" Value="Collapsed" />
                    <Setter TargetName="newtitle" Property="Visibility" Value="Visible" />
                </DataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding AppDisplayMode}" Value="{x:Static models:AppDisplayMode.EditPassword}"/>
                        <Condition Binding="{Binding IsPasswordGenVisible}" Value="false"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="controls:FocusContainer.IsFocused" Value="true" TargetName="nameBox" />
                </MultiDataTrigger>
                <DataTrigger Binding="{Binding AppDisplayMode}" Value="{x:Static models:AppDisplayMode.EditPassword}">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <!--<DoubleAnimation Storyboard.TargetName="passwordContentTransform" Storyboard.TargetProperty="X" From="-60" To="0" EasingFunction="{StaticResource ease}" Duration="0:0:0.15"/>-->
                                <DoubleAnimation Storyboard.TargetName="addFieldBtn" Storyboard.TargetProperty="Opacity"
                                        From="0" To="1" EasingFunction="{StaticResource ease}" Duration="0:0:0.35" />
                                <DoubleAnimation Storyboard.TargetName="addFieldTransform" Storyboard.TargetProperty="X"
                                        From="-50" To="0" EasingFunction="{StaticResource ease}" Duration="0:0:0.35" />
                                <DoubleAnimation Storyboard.TargetName="categoriesContentTransform"
                                        Storyboard.TargetProperty="X" From="300" To="0"
                                        EasingFunction="{StaticResource ease}" Duration="0:0:0.10" />
                                <DoubleAnimation Storyboard.TargetName="categoriesHeaderTransform"
                                        Storyboard.TargetProperty="X" From="300" To="0"
                                        EasingFunction="{StaticResource ease}" Duration="0:0:0.25" />
                                <DoubleAnimation Storyboard.TargetName="footerTransform" Storyboard.TargetProperty="Y"
                                        From="80" To="0" EasingFunction="{StaticResource ease}" Duration="0:0:0.25" />
                                <DoubleAnimation Storyboard.TargetName="appButtonsTransform"
                                        Storyboard.TargetProperty="X" From="30" To="0"
                                        EasingFunction="{StaticResource ease}" Duration="0:0:1" />
                                <DoubleAnimation Storyboard.TargetName="appButtons" Storyboard.TargetProperty="Opacity"
                                        From="0" To="1" EasingFunction="{StaticResource ease}" Duration="0:0:1" />
                                <DoubleAnimation Storyboard.TargetName="categoriesHeader"
                                        Storyboard.TargetProperty="Opacity" From="0" To="1"
                                        EasingFunction="{StaticResource ease}" Duration="0:0:0.25" />
                                <DoubleAnimation Storyboard.TargetName="passwordContent"
                                        Storyboard.TargetProperty="Opacity" From="0" To="1"
                                        EasingFunction="{StaticResource ease}" Duration="0:0:0.25" />
                                <DoubleAnimation Storyboard.TargetName="categoriesContent"
                                        Storyboard.TargetProperty="Opacity" From="0" To="1"
                                        EasingFunction="{StaticResource ease}" Duration="0:0:0.25" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

    </UserControl.Resources>
    <UserControl.Style>
        <Style TargetType="UserControl">
            <!--<Setter Property="Template" Value="{StaticResource template}"/>-->
            <Style.Triggers>
                <DataTrigger Binding="{Binding AppDisplayMode}" Value="{x:Static models:AppDisplayMode.EditPassword}" >
                    <Setter Property="Template" Value="{StaticResource template}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>
</UserControl>
